<template>
  <div>
    <el-table style="width: 100%" border="" :data="records">
      <el-table-column type="index" label="序号" align="center" width="80">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column label="默认图片" width="120">
        <template slot-scope="{row,$index}">
          <img :src="row.skuDefaultImg" style="width:100px;height:100px;">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="120">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row,$index}">
          <hint-button type="success" icon="el-icon-bottom" size="mini" title="下线" v-if="row.isSale == 1"
            @click="cancelSale(row)"></hint-button>
          <hint-button type="info" icon="el-icon-top" size="mini" title="上线" v-else @click="onSale(row)"></hint-button>
          <hint-button type="primary" icon="el-icon-edit" size="mini" title="编辑" @click="edit(row)"></hint-button>
          <hint-button type="info" icon="el-icon-info" size="mini" title="查看" @click="getSkuInfo(row)"></hint-button>
          <el-popconfirm :title="`确定删除${row.skuName}吗？`" @onConfirm="remove(row)">
            <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除" slot="reference"></hint-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[5, 10, 20]" :page-size="limit" layout="prev, pager, next, jumper, ->, sizes, total" :total="total"
      style="text-align:center;">
    </el-pagination>

    <el-drawer :visible.sync="drawer" direction="rtl" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" v-for="(skuAttr, index) in skuInfo.skuAttrValueList" :key="skuAttr.id"
              style="margin-right: 10px;">{{ skuAttr.attrId }} - {{ skuAttr.valueId }} </el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel>
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      records: [],
      skuInfo: {},
      drawer: false

    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList() {
      const { page, limit } = this
      let result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code == 200) {
        this.records = result.data.records
        this.total = result.data.total

      }
    },
    handleCurrentChange(val) {
      this.page = val
      this.getSkuList()
    },
    handleSizeChange(val) {
      this.limit = val
      this.getSkuList()
    },
    //上架
    async onSale(row) {
      const result = await this.$API.sku.reqOnSale(row.id)
      if (result.code == 200) {
        row.isSale = 1
        this.$message({ type: 'success', message: '上线成功' })
      }

    },
    // 下架
    async cancelSale(row) {
      const result = await this.$API.sku.reqCancelSale(row.id)
      if (result.code == 200) {
        row.isSale = 0
        this.$message({ type: 'success', message: '下线成功' })
      }

    },
    edit(row) {
      this.$message('正在开发中...')
    },
    //查看按钮
    async getSkuInfo(row) {
      const result = await this.$API.sku.reqGetSkuById(row.id)
      if (result.code == 200) {
        this.skuInfo = result.data
        this.drawer = true
      }
    },

    // 删除按钮
    async remove(row) {
      const result = await this.$API.sku.reqDeleteSku(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功！' })
        this.getSkuList()
      } else {
        this.$message('删除失败！')
      }
    },
  }

}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}


</style>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;


}

.el-col {
  margin: 10px 10px;
}

>>>.el-carousel__button {
  width: 10px;
  height: 10px;
  background: red;
  border-radius: 50%;

}

</style>
